# -*- coding = utf-8 -*-
# @Time : 2022/1/20 9:21
# @Author : 龙王赘婿_彪少
# @File : pngtopdf2.py
# @Software: PyCharm

from reportlab.lib.pagesizes import portrait
from reportlab.pdfgen import canvas
from PIL import Image
import os


class Hudou:
    def __init__(self):
        self.dell()

        # "C:/Users/ThinkPad/Desktop/tag16/tag16_05_00000.png"
        # "C:/Users/ThinkPad/Desktop/tag16_end/demo.png"

    def readfile(self, path):
        self.img = Image.open(path)
        # print(self.img.size)
        # self.img.show()

    def changeimage(self, path):
        box = (1, 1, 7, 7)
        self.imgend = self.img.crop(box)
        self.imgend.save(path)
        # print(self.imgend.size)
        # self.imgend.show()

    def saveimage(self):
        # A4 宽度w = 210 高度 h = 297
        # 图片 像素 6x6 放大 40*40 分辨率 6/40
        # A4 像素 = 尺寸 * 分辨率   210 * 6 / 40 = 31.5   279 * 6 / 40 = 41.85
        self.pdf = canvas.Canvas("C:/Users/ThinkPad/Desktop/tag16_end/demo.pdf", pagesize=portrait((210, 297)))
        self.pdf.drawImage("C:/Users/ThinkPad/Desktop/tag16_end/demo.png", (210-40)/2, (297-40)/2, 40, 40)
        self.pdf.showPage()
        self.pdf.save()

    def dell(self):
        path = "C:/Users/ThinkPad/Desktop/tag16/"
        path2 = "C:/Users/ThinkPad/Desktop/tag16_end/"
        files = os.listdir(path)
        for file in files:
            self.readfile(path + str(file))
            self.changeimage(path2 + str(file))



if __name__ == "__main__":
    obj = Hudou()
